package com.cqc.preferenceactivity01;

import android.content.SharedPreferences;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.os.Bundle;
import android.preference.SwitchPreference;

/**
 * 当item多余一个屏幕时，会像ListView可以滑动的
 * <p>
 * LogUtil.d("key=" + key + ",value=" +value_cb2 );
 */
public class MainActivity extends PreferenceActivity implements Preference.OnPreferenceClickListener, SharedPreferences.OnSharedPreferenceChangeListener {

    private ListPreference listPre;
    private SwitchPreference switch1;
    private SwitchPreference switch2;

    Preference.OnPreferenceChangeListener listener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object object) {
            if (preference instanceof ListPreference) {
                listPre.setSummary(object.toString());
            }
            return false;
        }
    };
    private CheckBoxPreference cb1;
    private EditTextPreference et1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        addPreferencesFromResource(R.xml.pre_list);
        listPre = (ListPreference) findPreference("listPre");
        listPre.setSummary(listPre.getValue());
        listPre.setOnPreferenceClickListener(this);
//        listPre.setOnPreferenceChangeListener(listener);
//        listPre.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//
//            @Override
//            public boolean onPreferenceChange(Preference pref, Object object) {
//                String key = pref.getKey();
//                String value = object.toString();
//                LogUtil.d("ListPreference setOnPreferenceChangeListener key=" + key+",value="+value);
//                int index = listPre.findIndexOfValue(value);
//                String  entry = listPre.getEntries()[index].toString();
//                CharSequence[] entryValues = listPre.getEntryValues();
//                listPre.setSummary(value);
//                return true;//false:不会保存change结果
//            }
//        });
        LogUtil.d("listPre.getValue()=" + listPre.getValue());//获取ListPreference选中的值

        addPreferencesFromResource(R.xml.pre_check_box);
        addPreferencesFromResource(R.xml.pre_edit_text);
        addPreferencesFromResource(R.xml.pre_switch);
        addPreferencesFromResource(R.xml.pre_ringtone);//点击会打开Ringtone
        addPreferencesFromResource(R.xml.pre_multi_list);//Todo
        addPreferencesFromResource(R.xml.pre_screen);
        addPreferencesFromResource(R.xml.pre_categroy);


        cb1 = (CheckBoxPreference) findPreference("cb1");
        et1 = (EditTextPreference) findPreference("et1");
        switch1 = (SwitchPreference) findPreference("switch1");
        switch2 = (SwitchPreference) findPreference("switch2");

        et1.setSummary(et1.getText().trim());
    }

    @Override
    protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceClick(Preference pref) {
        String key = pref.getKey();
        String value = pref.getSharedPreferences().getString(key, "");
        LogUtil.d("list_pre key=" + key + ",value=" + value);
        switch (pref.getKey()) {
            case "listPre":
                break;
        }
        return false;
    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {//sharedPreferences

        switch (key) {
            case "listPre":
                String value = prefs.getString(key, "");
                listPre.setSummary(value);

                break;
            case "cb1":
                boolean value_cb1 = prefs.getBoolean(key, false);
                break;
            case "cb2":
                boolean value_cb2 = prefs.getBoolean(key, false);
                break;
            case "et1":
                String value_et1 = prefs.getString(key, "");
                et1.setSummary(prefs.getString(key, value_et1));
                break;
            case "switch1":
                //错误
//                boolean value_st1 = prefs.getBoolean(key, false);
                break;
            case "switch2":
                break;
        }
    }
}
